﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import Engine.Events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import com.facebook.*;
    import com.facebook.events.*;
    import com.facebook.net.*;
    import com.facebook.data.*;
    import com.facebook.session.*;
    import com.facebook.data.users.*;
    import com.facebook.data.friends.*;
    import Engine.*;
    import com.facebook.commands.friends.*;
    import com.facebook.commands.users.*;
    import flash.external.*;

    public class FacebookUtil extends SocialNetwork {

        protected var m_facebookSession:IFacebookSession;
        protected var m_facebook:Facebook;
        protected var m_waitingForLogin:Boolean = false;
        protected var m_facebookUser:FacebookUser;
        protected var m_facebookFriends:Array;
        protected var m_facebookAppFriends:Array;
        protected var m_needsLogin:Boolean = false;
        protected var m_offlineMode:Boolean;
        protected var m_offlineModeId:Number;

        public function FacebookUtil(_arg1:String, _arg2:String){
            var _local5:Boolean;
            var _local6:Number;
            super();
            var _local3:String;
            var _local4:Object = GlobalEngine.getFlashVars();
            if (_local4.fb_sig_session_key != null){
                _local3 = _local4.fb_sig_session_key;
            };
            if (((((_local4.fb_sig_ss) && (_local4.fb_sig_api_key))) && (_local4.fb_sig_session_key))){
                this.m_facebookSession = new WebSession(_local4.fb_sig_api_key, _local4.fb_sig_ss, _local4.fb_sig_session_key);
                (this.m_facebookSession as WebSession).facebook_internal::_uid = _local4.fb_sig_user;
            } else {
                if (_local4.as_app_name){
                    this.m_facebookSession = new JSSession(_arg1, _local4.as_app_name);
                } else {
                    this.m_facebookSession = new DesktopSession(_arg1, _arg2);
                };
            };
            this.m_facebookSession.session_key = _local3;
            this.m_facebook = new Facebook();
            this.m_facebook.addEventListener(FacebookEvent.WAITING_FOR_LOGIN, this.onWaitingForLogin);
            this.m_facebook.addEventListener(FacebookEvent.CONNECT, this.onConnect);
            this.m_facebook.startSession(this.m_facebookSession);
            if (_local4.fb_sig_session_key){
                this.m_facebookSession.verifySession();
                this.m_needsLogin = false;
                m_sigTime = _local4.fb_sig_time;
            } else {
                _local5 = false;
                if (_local4.decouple_from_facebook){
                    GlobalEngine.log("Facebook", "Attempting to decouple from facebook");
                    _local6 = parseFloat(_local4.decoupled_facebook_id);
                    if (isNaN(_local6)){
                        GlobalEngine.log("Facebook", "Can't decouple from facebook, invalid ID");
                        _local5 = false;
                    } else {
                        this.m_offlineModeId = _local6;
                        _local5 = true;
                        GlobalEngine.log("Facebook", ("Succesfully decoupled from facebook.  OfflineId: " + this.m_offlineModeId));
                    };
                };
                if (_local5){
                    this.m_needsLogin = false;
                    this.m_offlineMode = true;
                } else {
                    this.m_facebook.addEventListener(FacebookEvent.WAITING_FOR_LOGIN, this.onWaitingForLogin);
                    this.m_facebook.addEventListener(FacebookEvent.CONNECT, this.onConnect);
                    this.m_facebook.login(true);
                    this.m_needsLogin = true;
                };
            };
        }
        public function get needsLogin():Boolean{
            return (this.m_needsLogin);
        }
        public function get facebook():Facebook{
            return (this.m_facebook);
        }
        private function onWaitingForLogin(_arg1:FacebookEvent):void{
            this.m_waitingForLogin = true;
            this.m_facebook.removeEventListener(FacebookEvent.WAITING_FOR_LOGIN, this.onWaitingForLogin);
        }
        public function validateLogin():void{
            if (this.m_waitingForLogin){
                this.m_facebookSession.refreshSession();
            };
        }
        private function onConnect(_arg1:FacebookEvent):void{
            if (((_arg1) && (_arg1.data))){
                this.m_facebook.removeEventListener(FacebookEvent.CONNECT, this.onConnect);
                dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.LOGIN_COMPLETE));
            };
        }
        public function getUser():void{
            var facebookData:* = null;
            var key:* = null;
            var getUserCall:* = null;
            try {
                facebookData = ExternalInterface.call("getUserInfo");
                if (((!((facebookData == null))) && (facebookData.hasOwnProperty("uid")))){
                    this.m_facebookUser = new FacebookUser();
                    facebookData["uid"] = facebookData["snuid"];
                    for (key in facebookData) {
                        if (key != "snuid"){
                            this.m_facebookUser[key] = facebookData[key];
                        };
                    };
                };
            } catch(err:Error) {
                if (err){
                    GlobalEngine.log("Facebook", ("getUserInfo threw exception: " + err.message));
                };
            };
            if (((!((this.m_facebookUser == null))) && ((this.m_facebookUser.uid == "")))){
                this.m_facebookUser = null;
            };
            if ((((this.m_facebookUser == null)) && (!(this.m_offlineMode)))){
                GlobalEngine.log("Facebook", "Unable to load user info from JS, using REST...");
                if (this.m_facebook.uid != null){
                    getUserCall = this.m_facebook.post(new GetInfo([this.m_facebook.uid], ["uid", "name", "first_name", "pic_square", "sex"]));
                    getUserCall.addEventListener(FacebookEvent.COMPLETE, this.onGetUser);
                } else {
                    ErrorManager.addError("Facebook uid is null in getUser", ErrorManager.ERROR_FAILED_TO_LOAD);
                };
            } else {
                dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_USER_COMPLETE));
            };
            this.getExtendedPermissions();
        }
        protected function getExtendedPermissions():void{
            var _local1:FacebookCall = this.m_facebook.post(new HasAppPermission("publish_stream", this.m_facebook.uid));
            _local1.addEventListener(FacebookEvent.COMPLETE, this.onGetAppPermissionComplete);
            var _local2:FacebookCall = this.m_facebook.post(new HasAppPermission("email", this.m_facebook.uid));
            _local2.addEventListener(FacebookEvent.COMPLETE, this.onGetAppEmailPermissionComplete);
        }
        private function onGetAppPermissionComplete(_arg1:FacebookEvent):void{
            if (((((_arg1) && (_arg1.success))) && (_arg1.data))){
                m_hasPublishPermissions = (_arg1.data as BooleanResultData).value;
            };
        }
        private function onGetAppEmailPermissionComplete(_arg1:FacebookEvent):void{
            if (((((_arg1) && (_arg1.success))) && (_arg1.data))){
                m_hasEmailPermission = (_arg1.data as BooleanResultData).value;
            };
        }
        private function onGetUser(_arg1:FacebookEvent):void{
            if (((((_arg1) && (_arg1.data))) && (!((GetInfoData(_arg1.data).userCollection.getItemAt(0) == null))))){
                this.m_facebookUser = FacebookUser(GetInfoData(_arg1.data).userCollection.getItemAt(0));
                dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_USER_COMPLETE));
            };
        }
        override public function getLoggedInUserId():String{
            var _local1:String;
            if (this.m_facebook){
                _local1 = this.m_facebook.uid;
            } else {
                if (this.m_offlineMode){
                    _local1 = this.m_offlineModeId.toString();
                };
            };
            return (_local1);
        }
        override public function getLoggedInUser():SocialNetworkUser{
            return (this.toSocialNetworkUser(this.m_facebookUser));
        }
        public function getFriendList():void{
            var getFriendListCall:* = null;
            try {
                this.m_facebookFriends = ExternalInterface.call("getFriendData");
            } catch(err:Error) {
                if (err){
                    GlobalEngine.log("Facebook", ("getFriendData threw exception: " + err.message));
                };
            };
            if ((((this.m_facebookFriends == null)) && ((this.m_offlineMode == false)))){
                GlobalEngine.log("Facebook", "Unable to load friend data from JS, using REST...");
                getFriendListCall = this.m_facebook.post(new GetFriends());
                getFriendListCall.addEventListener(FacebookEvent.COMPLETE, this.onGetFriendList);
            } else {
                dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_FRIENDS_COMPLETE));
            };
        }
        private function onGetFriendList(_arg1:FacebookEvent):void{
            var _local4:int;
            var _local2:GetFriendsData = GetFriendsData(_arg1.data);
            var _local3:Array = [];
            if (_local2 != null){
                _local4 = 0;
                while (_local4 < _local2.friends.length) {
                    _local3.push(_local2.friends.getItemAt(_local4).uid);
                    _local4++;
                };
                this.getFriends(_local3);
            };
        }
        protected function getFriends(_arg1:Array):void{
            var _local2:FacebookCall = this.m_facebook.post(new GetInfo(_arg1, ["uid", "name", "first_name", "pic_square", "sex"]));
            _local2.addEventListener(FacebookEvent.COMPLETE, this.onGetFriends);
        }
        private function onGetFriends(_arg1:FacebookEvent):void{
            var _local3:int;
            var _local4:FacebookUser;
            this.m_facebookFriends = [];
            var _local2:GetInfoData = GetInfoData(_arg1.data);
            if (_local2 != null){
                _local3 = 0;
                while (_local3 < _local2.userCollection.length) {
                    _local4 = (_local2.userCollection.source[_local3] as FacebookUser);
                    this.m_facebookFriends.push(_local4);
                    _local3++;
                };
            };
            dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_FRIENDS_COMPLETE));
        }
        public function getAppFriends():void{
            var getAppUsers:* = null;
            try {
                this.m_facebookAppFriends = ExternalInterface.call("getAppFriendIds");
            } catch(err:Error) {
                if (err){
                    GlobalEngine.log("Facebook", ("getAppFriendIds threw exception: " + err.message));
                };
            };
            if ((((this.m_facebookAppFriends == null)) && ((this.m_offlineMode == false)))){
                GlobalEngine.log("Facebook", "Unable to load appFriendIds from JS, using REST...");
                getAppUsers = this.m_facebook.post(new GetAppUsers());
                getAppUsers.addEventListener(FacebookEvent.COMPLETE, this.onGetAppFriends);
            } else {
                dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_APP_FRIENDS_COMPLETE));
            };
        }
        protected function onGetAppFriends(_arg1:FacebookEvent):void{
            var _local2:GetAppUserData = GetAppUserData(_arg1.data);
            if (_local2 != null){
                this.m_facebookAppFriends = _local2.uids;
            };
            dispatchEvent(new SocialNetworkEvent(SocialNetworkEvent.GET_APP_FRIENDS_COMPLETE));
        }
        override public function getFriendUsers():Array{
            var _local2:Array;
            var _local3:int;
            var _local4:SocialNetworkUser;
            var _local1:Array = [];
            if (this.m_facebookFriends){
                _local2 = this.m_facebookFriends;
                _local3 = 0;
                while (_local3 < _local2.length) {
                    _local4 = this.toSocialNetworkUser(_local2[_local3]);
                    _local1.push(_local4);
                    _local3++;
                };
            };
            return (_local1);
        }
        override public function getAppFriendsIds():Array{
            return (this.m_facebookAppFriends);
        }
        override public function showFeedDialog(_arg1:String, _arg2:Object, _arg3:Array, _arg4:Function=null):void{
            var bundleId:* = _arg1;
            var bundleData:* = _arg2;
            var targetIds:* = _arg3;
            var closeCallback = _arg4;
            try {
                if (ExternalInterface.available){
                    if (GlobalEngine.stage.displayState == StageDisplayState.FULL_SCREEN){
                        GlobalEngine.stage.displayState = StageDisplayState.NORMAL;
                    };
                    ExternalInterface.call("showFeedDialog", bundleId, bundleData, targetIds, closeCallback);
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
                if (closeCallback != null){
                    closeCallback();
                };
            };
        }
        override public function streamPublish(_arg1:Object, _arg2:Object, _arg3:String, _arg4:String=null, _arg5:Function=null, _arg6:Boolean=false, _arg7:String=""):void{
            var attachment:* = _arg1;
            var actionLink:* = _arg2;
            var targetId:* = _arg3;
            var userMessagePrompt = _arg4;
            var closeCallback = _arg5;
            var autoPublish:Boolean = _arg6;
            var userMessage:String = _arg7;
            try {
                if (ExternalInterface.available){
                    if (GlobalEngine.stage.displayState == StageDisplayState.FULL_SCREEN){
                        GlobalEngine.stage.displayState = StageDisplayState.NORMAL;
                    };
                    ExternalInterface.call("streamPublish", attachment, actionLink, targetId, userMessagePrompt, userHasStreamPermissions());
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
                if (closeCallback != null){
                    closeCallback();
                };
            };
        }
        override public function showStreamPermissions(_arg1:Function=null):void{
            var callback = _arg1;
            super.showStreamPermissions(callback);
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call("showStreamPermissions");
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        override public function showBookmarkDialog():void{
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call("showBookmarkDialog");
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        override public function showEmailPermission(_arg1:Function=null):void{
            var callback = _arg1;
            super.showEmailPermission(callback);
            try {
                if (ExternalInterface.available){
                    ExternalInterface.call("showEmailPermission");
                };
            } catch(err:Error) {
                ErrorManager.addError(("ExternalInterface exception: " + err.message));
            };
        }
        protected function toSocialNetworkUser(_arg1:Object):SocialNetworkUser{
            var _local2:SocialNetworkUser;
            if (this.m_offlineMode){
                _local2 = new SocialNetworkUser(this.m_offlineModeId, Constants.NO_NETWORK);
                _local2.firstName = "Decoupled";
                _local2.name = "Decoupled";
                _local2.picture = "";
            } else {
                if ((_arg1 is FacebookUser)){
                    _local2 = new SocialNetworkUser(_arg1.uid, Constants.FACEBOOK_NETWORK);
                } else {
                    _local2 = new SocialNetworkUser(_arg1.snuid, Constants.FACEBOOK_NETWORK);
                };
                _local2.firstName = _arg1.first_name;
                _local2.name = _arg1.name;
                _local2.picture = _arg1.pic_square;
                _local2.gender = _arg1.sex;
                _local2.locale = _arg1.locale;
            };
            return (_local2);
        }

    }
}//package Engine.Classes 
